﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //隐藏任务栏区图标
            this.ShowInTaskbar = false;
            //图标显示在托盘区
            notifyIcon1.Visible = true;

            sysTimer.Start();
        }

        private void sysTimer_Tick(object sender, EventArgs e)
        {
            string appStartPath = System.Windows.Forms.Application.StartupPath + "\\HiControlSystem.exe";
            string appName = "HiControlSystem";//the path of the exe file
            bool runFlag = false;
            Process[] myProcesses = Process.GetProcesses();
            foreach (Process myProcess in myProcesses)
            {
                if (myProcess.ProcessName.CompareTo(appName) == 0)
                {
                    runFlag = true;
                }

            }

            if (!runFlag)   //如果程序没有启动
            {
                Process proc = new Process();
                proc.StartInfo.FileName = appName;
                proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(appStartPath);
                proc.Start();
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示    
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点
                this.Activate();
                //任务栏区显示图标
                this.ShowInTaskbar = true;
                //托盘区图标隐藏
                notifyIcon1.Visible = false;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            sysTimer.Stop();
            // 关闭所有的线程
            this.Dispose();
            this.Close();
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {

        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示    
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点
                this.Activate();
                //任务栏区显示图标
                this.ShowInTaskbar = true;
                //托盘区图标隐藏
                notifyIcon1.Visible = false;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sysTimer.Stop();
            // 关闭所有的线程
            this.Dispose();
            this.Close();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            //判断是否选择的是最小化按钮
            if (WindowState == FormWindowState.Minimized)
            {
                //隐藏任务栏区图标
                this.ShowInTaskbar = false;
                //图标显示在托盘区
                notifyIcon1.Visible = true;
            }
        }
    }
}
